package com.mc.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mc.model.Menu;
import com.mc.model.Path;
import com.mc.model.SubMenu;
import com.mc.service.MenuService;
import com.mc.service.SubMenuService;
import com.mc.utils.PathUtil;

@Controller
@RequestMapping("/backend/menu")
public class MenuAction {
	//菜单相关持久层
	@Autowired private MenuService menuService;
	@Autowired private SubMenuService subMenuService;
	
	@RequestMapping("/manage")
	public String menuManage(ModelMap modelMap) {
		
		modelMap.addAttribute("menus", getMenus());
		
		Path path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:menumanage","ViewTitle:属性管理","smenu:menu","mmenu:menumanage"});				
		return PathUtil.returnStr(path, modelMap);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(@RequestParam int id, @RequestParam String name) {
		Menu menu = new Menu();
		menu.setId(id);
		menu.setName(name);
		int row = menuService.updateByPrimaryKeySelective(menu);
		if (row > 0)
			return "success";
		return "failed";
	}
	
	@RequestMapping("/submenuupdate")
	@ResponseBody
	public String updateSubMenu(@RequestParam int id, @RequestParam String name) {
		SubMenu subMenu = new SubMenu();
		subMenu.setId(id);
		subMenu.setName(name);
		int row = subMenuService.updateByPrimaryKeySelective(subMenu);
		if (row > 0)
			return "success";
		return "failed";
	}
	
	@RequestMapping("/add")
	public String addMenu(@RequestParam(value="fid", required=false) Integer fid, 
			@RequestParam(value="name", required=false) String name, ModelMap modelMap) {
		if (fid == null || name == null)
			return "forward:/backend/menu/manage";
		
		int row = 0;
		//如果fid为0，则说明添加的是顶级菜单
		if (fid == 0) {
			Menu menu = new Menu();
			menu.setName(name);
			row = menuService.insertSelective(menu);
		} else if (fid > 0) {
			//如果fid>0，则说明是添加的是二级菜单
			SubMenu subMenu = new SubMenu();
			Menu parentMenu = new Menu();
			parentMenu.setId(fid);
			subMenu.setParentMenu(parentMenu);
			subMenu.setName(name);
			row = subMenuService.insert(subMenu);
			
		}

		if (row > 0)
			modelMap.addAttribute("message", true);
		else 
			modelMap.addAttribute("message", false);
		
		return "forward:/backend/menu/manage";
	}
	
	/**删除顶菜单*/
	@RequestMapping("/del")
	@ResponseBody
	public String del(@RequestParam Integer id) {
		int row = menuService.deleteByPrimaryKey(id);
		if (row > 0)
			return "success";
		return "failed";
	}
	
	/**删除二级菜单*/
	@RequestMapping("/delSubMenu")
	@ResponseBody
	public String delSubMenu(@RequestParam Integer id) {
		int row = subMenuService.deleteByPrimaryKey(id);
		if (row > 0)
			return "success";
		return "failed";
	}
	
	
	//获取所有菜单项
	private List<Menu> getMenus() {
		List<Menu> menus = menuService.selectAllMenu();
		
		for (Menu menu : menus) {
			List<SubMenu> subMenus = subMenuService.selectSubMenuByMid(menu.getId());
			menu.setChilds(subMenus);
		}
		
		return menus;
	}
}
